using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

using TnPlus.Entities.Models;
using TnPlus.Entities.Utils;
using TnPlus.Common;

namespace TnPlus.DAL.Base
{
	public class CallSMSHistoryDAOBase
	{
		#region Fields

		protected string connectionStringName;

		#endregion

		#region Constructors

		public CallSMSHistoryDAOBase(string connectionStringName)
		{
			this.connectionStringName = connectionStringName;
		}

		#endregion

		#region Methods

		/// <summary>
		/// Saves a record to the CallSMSHistory table.
		/// </summary>
		public virtual void Insert(CallSMSHistoryInfo callSMSHistoryInfo)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdCallSMS", callSMSHistoryInfo.IdCallSMS),
				new SqlParameter("@PostDate", callSMSHistoryInfo.PostDate),
				new SqlParameter("@IdCommon", callSMSHistoryInfo.IdCommon),
				new SqlParameter("@AccountNo", callSMSHistoryInfo.AccountNo),
				new SqlParameter("@Asset", callSMSHistoryInfo.Asset),
				new SqlParameter("@Debt", callSMSHistoryInfo.Debt),
				new SqlParameter("@Ratio", callSMSHistoryInfo.Ratio),
				new SqlParameter("@CallLMV", callSMSHistoryInfo.CallLMV),
				new SqlParameter("@ForceLMV", callSMSHistoryInfo.ForceLMV),
				new SqlParameter("@Action", callSMSHistoryInfo.Action),
				new SqlParameter("@AddCash", callSMSHistoryInfo.AddCash),
				new SqlParameter("@ContentSMS", callSMSHistoryInfo.ContentSMS),
				new SqlParameter("@ReceiveNo", callSMSHistoryInfo.ReceiveNo),
				new SqlParameter("@Status", callSMSHistoryInfo.Status)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "CallSMSHistory_Insert", parameters);
		}

		/// <summary>
		/// Updates a record in the CallSMSHistory table.
		/// </summary>
		public virtual void Update(CallSMSHistoryInfo callSMSHistoryInfo)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdCallSMS", callSMSHistoryInfo.IdCallSMS),
				new SqlParameter("@PostDate", callSMSHistoryInfo.PostDate),
				new SqlParameter("@IdCommon", callSMSHistoryInfo.IdCommon),
				new SqlParameter("@AccountNo", callSMSHistoryInfo.AccountNo),
				new SqlParameter("@Asset", callSMSHistoryInfo.Asset),
				new SqlParameter("@Debt", callSMSHistoryInfo.Debt),
				new SqlParameter("@Ratio", callSMSHistoryInfo.Ratio),
				new SqlParameter("@CallLMV", callSMSHistoryInfo.CallLMV),
				new SqlParameter("@ForceLMV", callSMSHistoryInfo.ForceLMV),
				new SqlParameter("@Action", callSMSHistoryInfo.Action),
				new SqlParameter("@AddCash", callSMSHistoryInfo.AddCash),
				new SqlParameter("@ContentSMS", callSMSHistoryInfo.ContentSMS),
				new SqlParameter("@ReceiveNo", callSMSHistoryInfo.ReceiveNo),
				new SqlParameter("@Status", callSMSHistoryInfo.Status)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "CallSMSHistory_Update", parameters);
		}

		/// <summary>
		/// Deletes a record from the CallSMSHistory table by its primary key.
		/// </summary>
		public virtual void Delete(Guid? idCallSMS)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdCallSMS", idCallSMS)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "CallSMSHistory_Delete", parameters);
		}

		/// <summary>
		/// Selects a single record from the CallSMSHistory table.
		/// </summary>
		public virtual CallSMSHistoryInfo Select(Guid? idCallSMS)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdCallSMS", idCallSMS)
			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "CallSMSHistory_Select", parameters))
			{
				if (dataReader.Read())
				{
					return MakeCallSMSHistoryInfo(dataReader);
				}
				else
				{
					return null;
				}
			}
		}

		/// <summary>
		/// Selects all records from the CallSMSHistory table.
		/// </summary>
		public List<CallSMSHistoryInfo> SelectAll()
		{
			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "CallSMSHistory_SelectAll"))
			{
				List<CallSMSHistoryInfo> callSMSHistoryInfoList = new List<CallSMSHistoryInfo>();
				while (dataReader.Read())
				{
					callSMSHistoryInfoList.Add(MakeCallSMSHistoryInfo(dataReader));
				}

				return callSMSHistoryInfoList;
			}
		}

		/// <summary>
		/// Creates a new instance of the CallSMSHistory class and populates it with data from the specified SqlDataReader.
		/// </summary>
		protected virtual CallSMSHistoryInfo MakeCallSMSHistoryInfo(SqlDataReader dataReader)
		{
			CallSMSHistoryInfo callSMSHistoryInfo = new CallSMSHistoryInfo();
			callSMSHistoryInfo.IdCallSMS = SqlClientUtility.GetGuid(dataReader, DbConstants.CALLSMSHISTORY.IDCALLSMS);
			callSMSHistoryInfo.PostDate = SqlClientUtility.GetDateTime(dataReader, DbConstants.CALLSMSHISTORY.POSTDATE);
			callSMSHistoryInfo.IdCommon = SqlClientUtility.GetString(dataReader, DbConstants.CALLSMSHISTORY.IDCOMMON);
			callSMSHistoryInfo.AccountNo = SqlClientUtility.GetString(dataReader, DbConstants.CALLSMSHISTORY.ACCOUNTNO);
			callSMSHistoryInfo.Asset = SqlClientUtility.GetDecimal(dataReader, DbConstants.CALLSMSHISTORY.ASSET);
			callSMSHistoryInfo.Debt = SqlClientUtility.GetDecimal(dataReader, DbConstants.CALLSMSHISTORY.DEBT);
			callSMSHistoryInfo.Ratio = SqlClientUtility.GetDecimal(dataReader, DbConstants.CALLSMSHISTORY.RATIO);
			callSMSHistoryInfo.CallLMV = SqlClientUtility.GetDecimal(dataReader, DbConstants.CALLSMSHISTORY.CALLLMV);
			callSMSHistoryInfo.ForceLMV = SqlClientUtility.GetDecimal(dataReader, DbConstants.CALLSMSHISTORY.FORCELMV);
			callSMSHistoryInfo.Action = SqlClientUtility.GetString(dataReader, DbConstants.CALLSMSHISTORY.ACTION);
			callSMSHistoryInfo.AddCash = SqlClientUtility.GetDecimal(dataReader, DbConstants.CALLSMSHISTORY.ADDCASH);
			callSMSHistoryInfo.ContentSMS = SqlClientUtility.GetString(dataReader, DbConstants.CALLSMSHISTORY.CONTENTSMS);
			callSMSHistoryInfo.ReceiveNo = SqlClientUtility.GetString(dataReader, DbConstants.CALLSMSHISTORY.RECEIVENO);
			callSMSHistoryInfo.Status = SqlClientUtility.GetBoolean(dataReader, DbConstants.CALLSMSHISTORY.STATUS);

			return callSMSHistoryInfo;
		}

		#endregion
	}
}
